<template>
  <el-table :data="list" border style="width: 100%;">
    <template v-for="column in items" :key="column.prop">
      <el-table-column :prop="column.prop" :label="column.label" :width="column.width" />
    </template>
    <el-table-column label="操作" fixed="right" align="center" width="150">
      <template #default="scope">
        <el-button size="mini" type="primary" @click="$emit('updateRow', scope.row)">
          修改
        </el-button>
        <el-button size="mini" type="danger" @click="handleDeleteRow(scope)">
          删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
    :page-sizes="[10, 20, 50, 100]"
    layout="total, sizes, prev, pager, next, jumper"
    :current-page="pageCurrent"
    :page-size="pageSize"
    :total="total"
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
  />
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import { useMessageBox } from '@/hooks/useMessage'
import { removeApi } from '@/api/component/searchTable'
export default defineComponent({
  name: 'TableModule',
  props: {
    // 模型url
    modelUrl: {
      type: String,
      required: true,
    },
    // 表格列渲染列表
    items: {
      type: Array,
      required: true,
    },
    // 表格数据
    list: {
      type: Array,
      default: () => {
        return []
      },
    },
    pageCurrent: {
      type: Number,
      default: 1,
    },
    pageSize: {
      type: Number,
      default: 10,
    },
    total: {
      type: Number,
      default: 0,
    },
  },
  emits: ['updateRow', 'removeBack', 'pageChange'],
  setup(props, context) {
    const handleDeleteRow = (scope) => {
      useMessageBox.warning('此操作将永久删除数据，是否继续?').then(() => {
        removeApi(`${props.modelUrl}/${scope.row.id}`).then(() => {
          context.emit('removeBack')
        })
      })
    }
    // 每页数量改变事件
    function handleSizeChange(value) {
      context.emit('pageChange', { pageSize: value })
    }
    // 页码改变事件
    function handleCurrentChange(value) {
      context.emit('pageChange', { pageCurrent: value })
    }

    return {
      handleDeleteRow,
      handleSizeChange,
      handleCurrentChange,

    }
  },
})
</script>

<style lang="scss" scoped>
//
</style>
